package com.ssy.jt808.gateway.sdk.terminal.converter;


import com.ssy.jt808.gateway.sdk.terminal.constant.location.LocationAlarmBitConstant;
import com.ssy.jt808.gateway.sdk.terminal.constant.location.LocationAlarmEnum;
import com.ssy.jt808.gateway.sdk.terminal.constant.location.LocationStatusBitConstant;
import com.ssy.jt808.gateway.sdk.terminal.pojo.info.LocationStatusInfo;
import com.ssy.jt808.gateway.sdk.terminal.pojo.info.alarm.LocationAlarmStateInfo;
import com.ssy.jt808.gateway.sdk.terminal.utils.BitUtil;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import static com.ssy.jt808.gateway.sdk.terminal.utils.BitUtil.existFlag;

/**
 * @author wangzezhou
 */
public class TerminalLocationConverter{

  /** 转换成枚举 */
  public static List<LocationAlarmEnum> getAlarmList( int alarmBits ){

    if( alarmBits == 0 ){
      return Collections.emptyList();
    }

    List<LocationAlarmEnum> alarmEnums = new LinkedList<>();
    LocationAlarmEnum[] alarms = LocationAlarmEnum.values();
    for(LocationAlarmEnum alarm : alarms){
      if( existFlag(alarmBits, alarm.bit()) ){
        alarmEnums.add(alarm);
      }
    }

    return alarmEnums;
  }

  /** 转换成报警对象 */
  public static LocationAlarmStateInfo convertLocationAlarmInfo( int alarmBits ){

    LocationAlarmStateInfo locationAlarmStateInfo = new LocationAlarmStateInfo();
    if( alarmBits == 0 ){
      return locationAlarmStateInfo;
    }

    locationAlarmStateInfo.setSos(BitUtil.existFlag(alarmBits, LocationAlarmBitConstant.SOS));
    locationAlarmStateInfo.setOverSpeed(BitUtil.existFlag(alarmBits,
                                                          LocationAlarmBitConstant.OVER_SPEED));
    locationAlarmStateInfo.setFatigueDriving(BitUtil.existFlag(alarmBits,
                                                               LocationAlarmBitConstant.FATIGUE_DRIVING));
    locationAlarmStateInfo.setDangerWarning(BitUtil.existFlag(alarmBits,
                                                              LocationAlarmBitConstant.DANGER_WARNING));
    locationAlarmStateInfo.setGnssFailure(BitUtil.existFlag(alarmBits,
                                                            LocationAlarmBitConstant.GNSS_FAILURE));
    locationAlarmStateInfo.setGnssAntennaeFailure(BitUtil.existFlag(alarmBits,
                                                                    LocationAlarmBitConstant.GNSS_ANTENNAE_FAILURE));
    locationAlarmStateInfo.setGnssAntennaeCircuit(BitUtil.existFlag(alarmBits,
                                                                    LocationAlarmBitConstant.GNSS_ANTENNAE_CIRCUIT));
    locationAlarmStateInfo.setUnderPowered(BitUtil.existFlag(alarmBits,
                                                             LocationAlarmBitConstant.UNDER_POWERED));
    locationAlarmStateInfo.setPowerDown(BitUtil.existFlag(alarmBits,
                                                          LocationAlarmBitConstant.POWER_DOWN));
    locationAlarmStateInfo.setDisplayFailure(BitUtil.existFlag(alarmBits,
                                                               LocationAlarmBitConstant.DISPLAY_FAILURE));
    locationAlarmStateInfo.setTtsFailure(BitUtil.existFlag(alarmBits,
                                                           LocationAlarmBitConstant.TTS_FAILURE));
    locationAlarmStateInfo.setCameraFailure(BitUtil.existFlag(alarmBits,
                                                              LocationAlarmBitConstant.CAMERA_FAILURE));
    locationAlarmStateInfo.setTransportIcFailure(BitUtil.existFlag(alarmBits,
                                                                   LocationAlarmBitConstant.TRANSPORT_IC_FAILURE));
    locationAlarmStateInfo.setOverSpeedWarning(BitUtil.existFlag(alarmBits,
                                                                 LocationAlarmBitConstant.OVER_SPEED_WARNING));
    locationAlarmStateInfo.setFatigueDrivingWarning(BitUtil.existFlag(alarmBits,
                                                                      LocationAlarmBitConstant.FATIGUE_DRIVING_WARNING));
    locationAlarmStateInfo.setDrivingOvertime(BitUtil.existFlag(alarmBits,
                                                                LocationAlarmBitConstant.DRIVING_OVERTIME));
    locationAlarmStateInfo.setStopOvertime(BitUtil.existFlag(alarmBits,
                                                             LocationAlarmBitConstant.STOP_OVERTIME));
    locationAlarmStateInfo.setInOutArea(BitUtil.existFlag(alarmBits,
                                                          LocationAlarmBitConstant.IN_OUT_AREA));
    locationAlarmStateInfo.setInOutPath(BitUtil.existFlag(alarmBits,
                                                          LocationAlarmBitConstant.IN_OUT_PATH));
    locationAlarmStateInfo.setDrivingTimeWarningOfPath(BitUtil.existFlag(alarmBits,
                                                                         LocationAlarmBitConstant.RouteDriveTimeAlarm));
    locationAlarmStateInfo.setDeviationPath(BitUtil.existFlag(alarmBits,
                                                              LocationAlarmBitConstant.DEVIATION_PATH));
    locationAlarmStateInfo.setVssFailure(BitUtil.existFlag(alarmBits,
                                                           LocationAlarmBitConstant.VSS_FAILURE));
    locationAlarmStateInfo.setFuelCapacity(BitUtil.existFlag(alarmBits,
                                                             LocationAlarmBitConstant.FUEL_CAPACITY_ANOMALY));
    locationAlarmStateInfo.setStealing(BitUtil.existFlag(alarmBits,
                                                         LocationAlarmBitConstant.STEALING));
    locationAlarmStateInfo.setIllegalStart(BitUtil.existFlag(alarmBits,
                                                             LocationAlarmBitConstant.ILLEGAL_START));
    locationAlarmStateInfo.setIllegalDisplace(BitUtil.existFlag(alarmBits,
                                                                LocationAlarmBitConstant.ILLEGAL_DISPLACE));
    locationAlarmStateInfo.setCrashWarning(BitUtil.existFlag(alarmBits,
                                                             LocationAlarmBitConstant.CRASH_WARNING));
    locationAlarmStateInfo.setRolloverWarning(BitUtil.existFlag(alarmBits,
                                                                LocationAlarmBitConstant.ROLLOVER_WARNING));
    locationAlarmStateInfo.setIllegalOpenDoor(BitUtil.existFlag(alarmBits,
                                                                LocationAlarmBitConstant.ILLEGAL_OPEN_DOOR));

    return locationAlarmStateInfo;
  }

  /** 转换车辆状态 */
  public static LocationStatusInfo convertLocationStatusInfo( int statusBits ){

    LocationStatusInfo info = new LocationStatusInfo();
    if( statusBits == 0 ){
      return info;
    }

    info.setAccOn(BitUtil.existFlag(statusBits, LocationStatusBitConstant.ACC));
    info.setLocating(BitUtil.existFlag(statusBits, LocationStatusBitConstant.LOCATING));
    info.setLatDirection(statusBits&LocationStatusBitConstant.LAT_DIRECTION);
    info.setLngDirection(statusBits&LocationStatusBitConstant.LNG_DIRECTION);
    info.setOperating(BitUtil.existFlag(statusBits, LocationStatusBitConstant.OPERATING));
    info.setLoadStatus(statusBits&LocationStatusBitConstant.LOAD_STATUS);
    info.setFuelFailure(BitUtil.existFlag(statusBits, LocationStatusBitConstant.FUEL_FAILURE));
    info.setElectricalFailure(BitUtil.existFlag(statusBits, LocationStatusBitConstant.ELECTRICAL_FAILURE));
    info.setLocked(BitUtil.existFlag(statusBits, LocationStatusBitConstant.LOCKED));
    info.setFrontDoorOpened(BitUtil.existFlag(statusBits, LocationStatusBitConstant.FRONT_DOOR_OPENED));
    info.setMiddleDoorOpened(BitUtil.existFlag(statusBits,
                                               LocationStatusBitConstant.MIDDLE_DOOR_OPENED));
    info.setBackDoorOpened(BitUtil.existFlag(statusBits,
                                             LocationStatusBitConstant.BACK_DOOR_OPENED));
    info.setDriverDoorOpened(BitUtil.existFlag(statusBits,
                                               LocationStatusBitConstant.DRIVER_DOOR_OPENED));
    info.setGpsLocating(BitUtil.existFlag(statusBits, LocationStatusBitConstant.GPS_LOCATING));
    info.setBeiDouLocating(BitUtil.existFlag(statusBits,
                                             LocationStatusBitConstant.BEI_DOU_LOCATING));
    info.setGlonassLocating(BitUtil.existFlag(statusBits,
                                              LocationStatusBitConstant.GLONASS_LOCATING));
    info.setGalileoLocating(BitUtil.existFlag(statusBits,
                                              LocationStatusBitConstant.GALILEO_LOCATING));

    return info;
  }


  private static final int[] ALARM_BITS = { LocationAlarmBitConstant.SOS,
                                            LocationAlarmBitConstant.OVER_SPEED,
                                            LocationAlarmBitConstant.FATIGUE_DRIVING,
                                            LocationAlarmBitConstant.DANGER_WARNING,
                                            LocationAlarmBitConstant.GNSS_FAILURE,
                                            LocationAlarmBitConstant.GNSS_ANTENNAE_FAILURE,
                                            LocationAlarmBitConstant.GNSS_ANTENNAE_CIRCUIT,
                                            LocationAlarmBitConstant.UNDER_POWERED,
                                            LocationAlarmBitConstant.POWER_DOWN,
                                            LocationAlarmBitConstant.DISPLAY_FAILURE,
                                            LocationAlarmBitConstant.TTS_FAILURE,
                                            LocationAlarmBitConstant.CAMERA_FAILURE,
                                            LocationAlarmBitConstant.TRANSPORT_IC_FAILURE,
                                            LocationAlarmBitConstant.OVER_SPEED_WARNING,
                                            LocationAlarmBitConstant.FATIGUE_DRIVING_WARNING,
                                            LocationAlarmBitConstant.DRIVING_OVERTIME,
                                            LocationAlarmBitConstant.STOP_OVERTIME,
                                            LocationAlarmBitConstant.IN_OUT_AREA,
                                            LocationAlarmBitConstant.IN_OUT_PATH,
                                            LocationAlarmBitConstant.RouteDriveTimeAlarm,
                                            LocationAlarmBitConstant.DEVIATION_PATH,
                                            LocationAlarmBitConstant.VSS_FAILURE,
                                            LocationAlarmBitConstant.FUEL_CAPACITY_ANOMALY,
                                            LocationAlarmBitConstant.STEALING,
                                            LocationAlarmBitConstant.ILLEGAL_START,
                                            LocationAlarmBitConstant.ILLEGAL_DISPLACE,
                                            LocationAlarmBitConstant.CRASH_WARNING,
                                            LocationAlarmBitConstant.ROLLOVER_WARNING,
                                            LocationAlarmBitConstant.ILLEGAL_OPEN_DOOR };

  private static final int[] STATUS_BITS = { LocationStatusBitConstant.ACC,
                                             LocationStatusBitConstant.LOCATING,
                                             LocationStatusBitConstant.LAT_DIRECTION,
                                             LocationStatusBitConstant.LNG_DIRECTION,
                                             LocationStatusBitConstant.OPERATING,
                                             LocationStatusBitConstant.LOAD_STATUS,
                                             LocationStatusBitConstant.FUEL_FAILURE,
                                             LocationStatusBitConstant.ELECTRICAL_FAILURE,
                                             LocationStatusBitConstant.LOCKED,
                                             LocationStatusBitConstant.FRONT_DOOR_OPENED,
                                             LocationStatusBitConstant.MIDDLE_DOOR_OPENED,
                                             LocationStatusBitConstant.BACK_DOOR_OPENED,
                                             LocationStatusBitConstant.DRIVER_DOOR_OPENED,
                                             LocationStatusBitConstant.GPS_LOCATING,
                                             LocationStatusBitConstant.BEI_DOU_LOCATING,
                                             LocationStatusBitConstant.GLONASS_LOCATING,
                                             LocationStatusBitConstant.GALILEO_LOCATING,
                                             };
}
